home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr25 / wordy.zip / BINGO.C < prev    next >
C/C++ Source or Header  |  1995-04-30  |  7KB  |  250 lines

  1. /**************************************************************************/
  2. /*                               Bingo Utility                        */
  3. /*                                                            */
  4. /*                                 M\Cooper                            */
  5. /*                          3425 Chestnut Ridge Rd.                        */
  6. /*                        Grantsville, MD 21536-9801                    */
  7. /*                        --------------------------                    */
  8. /*                        Email:  thegrendel@aol.com                    */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package             */
  11. /*                                                            */    
  12. /**************************************************************************/
  13.  
  14.  
  15. /**********************************WORDTEST********************************/
  16. /*       Function tests if word is constructible from Letterset            */
  17. /*                 Args in: char *letterset, char *word                          */
  18. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  19. /**************************************************************************/
  20.  
  21. #include <conio.h>
  22. #include "srch.h"
  23.  
  24.  
  25. #define FILE_OPENING_ERROR 3
  26. #define FILENAME_MAXLEN 8
  27. #define CR "\n"
  28. #define FILE_SUFFIX ".bgo"
  29. #define MAXLEN 30
  30. #define LINE_LEN 80
  31. #define NOARGS 1
  32. #define INCREMENT 1
  33. #define SPACE ' '
  34. #define XOUT '@'
  35. #define WILDCARD '?'
  36. #define FILLCHAR '_'
  37. #define BINGO 7
  38. #define BUFFERSIZE 8192
  39.  
  40. char ad[] =
  41. "BINGO utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536-9801";
  42.  
  43.  
  44. void getword( char *lset );
  45. void center( char *strng );
  46.  
  47. typedef enum { FALSE, TRUE } Boolean;
  48.  
  49. void main( int argc, char **argv )
  50. {
  51.  
  52.    char letterset[ MAXLEN ];
  53.  
  54.      if( argc == NOARGS )
  55.         {
  56.         clrscr();
  57.         puts("Enter a LETTERSET to test ... ");
  58.         gets( letterset );
  59.         }
  60.      else
  61.         strcpy( letterset, *( argv + 1 ) );
  62.  
  63.      getword( letterset );
  64. }
  65.  
  66.  
  67.  
  68. Boolean wordtest( char *letterset, char *word )
  69. {
  70.     Boolean error_flag = TRUE;
  71.     static char dup_lset[ MAXLEN ];
  72.     register char *letpos;
  73.  
  74. //     dup_lset = strdup( letterset );
  75.      strcpy( dup_lset, letterset );
  76.          
  77.         while( *word )
  78.             {
  79.             if( ( letpos  = strchr( dup_lset, *word++ ) ) != NULL )
  80.                 *letpos = XOUT;     //As long as letter contained...
  81.       else
  82.          if( ( letpos = strchr( dup_lset, WILDCARD ) ) != NULL ) 
  83.             *letpos = XOUT;  //Or wildcard character...
  84.  
  85.             else
  86.                 { error_flag = FALSE; break; } //test fails (not contained)
  87.             }
  88.  
  89.         return( error_flag );
  90. }
  91.  
  92. /*************************************************************/
  93. void getword( char *letter_set )
  94. {
  95.  
  96.     char    l_set [ MAXLEN ],
  97.         word [ MAXLEN ],
  98.         tempstr [ MAXLEN + 1 ],
  99.         targetfile [ MAXLEN ],
  100.         bar [ LINE_LEN + 1 ],
  101.         double_bar [ LINE_LEN + 1 ],
  102.    Bnum [ BINGO + 2 ],
  103.    *ppos;
  104.  
  105.     FILE *fptr,
  106.         *tfile;
  107.     int fnamelen,
  108.      i;
  109.     long wcount = 0L;
  110.  
  111.        memset( bar, '-', LINE_LEN );
  112.        *( bar + LINE_LEN ) = NULL;
  113.        memset( double_bar, '=', LINE_LEN );
  114.        *( double_bar + LINE_LEN ) = NULL;
  115.  
  116.        /*************opening credits*************/
  117.        clrscr();
  118.        printf( double_bar );
  119.        strcpy( tempstr, ad );
  120.        center ( tempstr );
  121.        printf( tempstr );
  122.     printf( " " );  //Pad out line.
  123.        printf( double_bar );
  124.        printf( CR );
  125.        /****************************************/
  126.  
  127.  
  128.        strcpy ( l_set, letter_set );
  129.        strcat ( letter_set, CR );
  130.  
  131.        /*   Create name of file to store derived words in   */
  132.        /*********************************************************/
  133.        fnamelen = strlen( l_set );
  134.        if( fnamelen  > FILENAME_MAXLEN )
  135.           fnamelen = FILENAME_MAXLEN;
  136.        strncpy( targetfile, l_set, fnamelen );
  137.        *( targetfile + fnamelen ) = NULL;
  138.        //NULL-terminate string, so strcat works, ha, ha.
  139.  
  140.       for( i = 0; i < fnamelen; i++ )
  141.          if( *( targetfile + i ) == WILDCARD )
  142.              *( targetfile + i ) = FILLCHAR;
  143.  
  144.  
  145. //    if( ( ppos = strchr( targetfile, '?' ) ) != NULL ) 
  146. //          *ppos = '0';
  147. //           strcpy( targetfile, "wildcard" );
  148.  
  149. //    ppos = targetfile;
  150. //    while( *ppos != NULL )
  151. //       if( *ppos == '?' )
  152. //          {
  153. //          *ppos = FILLCHAR;
  154. //          ppos++;
  155. //          printf( "\7\7\7\7" );  /******DEBUG******/
  156. //          }
  157.  
  158.        strcat( targetfile, FILE_SUFFIX );
  159.        /*********************************************************/
  160.  
  161.        if( !( fptr = fopen( Wordfile, "rt" ) ) )
  162.          {
  163.          printf( "\7\7\7Cannot open Wordfile!" );
  164.          exit( FILE_OPENING_ERROR );
  165.          }
  166.       if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
  167.          exit( FILE_OPENING_ERROR );
  168.  
  169.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  170.          {
  171.          printf( "\7\7\7Cannot open file to save words in!" );
  172.          exit ( FILE_OPENING_ERROR + 2 );
  173.          }
  174.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  175.          exit( FILE_OPENING_ERROR );
  176.  
  177.        /**************'Wait' Message************/
  178.        printf( CR CR );
  179.        printf( "WORKING...\n\n" );
  180.        printf( "This will take from 10 seconds or less [fast 486 machine]\n" );
  181.        printf( "to 5 minutes or more [slow 8088 with old hard drive].\n\n" );
  182.        printf( "Now searching 99,000+ word file and writing file of valid bingos.\n\n" );
  183.        /*****************************************/
  184.  
  185.  
  186.  
  187.  
  188.  
  189.        sprintf( tempstr, "Bingos created from: %s\n", strupr( l_set ) );
  190.        center( tempstr );
  191.        fprintf( tfile, double_bar );
  192.       fprintf( tfile, CR );
  193.        fprintf( tfile, tempstr );
  194.        fprintf( tfile, double_bar );
  195.        fprintf( tfile, CR );
  196.  
  197.  
  198.          /*********************Main Loop*************/     
  199.           while( fgets( word, MAXLEN, fptr ) != NULL )
  200.  
  201.             if( wordtest( letter_set, word ) && strlen( word ) >= BINGO + 1 ) // >7
  202.                {
  203.                fprintf( tfile, "%s", word );
  204.                wcount++;
  205.                }
  206.           /*******************************************/
  207.  
  208.       if( wcount == INCREMENT )
  209.          strcpy( Bnum, "Bingo" );
  210.       else
  211.          strcpy( Bnum, "Bingos" );
  212.  
  213.           fprintf( tfile, bar );
  214.       fprintf( tfile, CR );
  215.           sprintf( tempstr, "%ld %s can be constructed from %s.",
  216.                  wcount, Bnum, l_set );
  217.           center( tempstr );              
  218.           fprintf( tfile, tempstr );
  219.           fprintf( tfile, "\n\n" );
  220.  
  221.           center( ad );
  222.           fprintf( tfile, ad );
  223.  
  224.           fcloseall();
  225.  
  226.           sprintf( tempstr,
  227.                  "The file %s has %ld %s derived from %s\7.",
  228.                  targetfile, wcount, Bnum, l_set );
  229.           center( tempstr );
  230.           printf( CR CR );
  231.           printf( tempstr );
  232.  
  233. }
  234.  
  235.  
  236.  
  237. void center( char *str )
  238. {
  239.    int padding;
  240.    char st [ LINE_LEN + INCREMENT ];
  241.  
  242.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  243.      memset( st, SPACE, padding );
  244.      *( st + padding ) = NULL;  //Terminate string
  245.      strcat( st, str );
  246.      strcpy( str, st );
  247.  
  248.      return;
  249. }
  250.